﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpDom;
using System.Web.Mvc.Html;
using Bzure.Common.Core;
using Bzure.Apps.Security.ViewModels;
using Bzure.Security.Permissions;

namespace Bzure.Apps.Security.Views
{
    public class DroitConfig : PageView<DroitConfigViewModel>
    {
        public virtual ContainerTag TitleContent(ContainerTag container)
        {
            return container[Model.Title];
        }

        string Script2(string url)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("var url =\"{0}\";", url);
            sb.Append(" var ConfigDroit = function () {");
            sb.Append(" if (confirm(\"你确定要改变角色授权吗？\")) {");
            sb.Append("var form = $(\"#DroitConfig_Form\");");
            sb.Append("var serializedForm = form.serialize();");
            sb.Append("$.post(url, serializedForm, function (result) {");
            sb.Append("$(\"#content_box_messages\").html(result);");
            sb.Append(" $(\".close\").click(");
            sb.Append("function () {");
            sb.Append(" $(this).fadeTo(400, 0, function () { ");
            sb.Append("$(this).slideUp(400);");
            sb.Append("   });");
            sb.Append("  return false;	});");
            sb.Append("});     } };");
            return sb.ToString();
        }
        bool IsChecked(TeamRole role, int key)
        {
            Bzure.Apps.Security.DroitResource
                   rs = this.Model.DRList.FirstOrDefault(m => m.RoleID == role.ID);
            List<int> RightValues = new List<int>();
            if (rs.IsNotNull())
            {
                RightValues.AddRange(rs.RightValue.ToFlags());
            }
            return rs.IsNotNull() && RightValues.Contains(key);
        }
        public virtual ContainerTag MainContent(ContainerTag container)
        {
            return container
            [
                style[
                 ".label_check{ min-width:80px;float:left;margin-left:10px}.authtd{width:560px;}"
                ],
                script.attr(type: MimeType.text.javascript, src: this.Url.RouteUrl("ResourceAccess_Script", new { location = "SecurityApp", id = "jquery-1.7.2.min.js" })),
                script.attr(type: MimeType.text.javascript)[Script2(Model.Url)],
                h2[Model.Title],
                p.attr(id: "page-intro")[" 配置" + Model.ResourceName + "的授权，您可以根据系统安全规则的需要来配置每个安全角色具有" + Model.ResourceName + "中的权限项或权限项组合。"],
                div.attr(@class: "clear"),
                div.attr(@class: "content-box")
                [
                     div.attr(@class: "content-box-header")
                      [
                            h3[Model.ResourceName],
                            ul.attr(@class: "content-box-tabs")
                            [
                               li[a.attr(href: "#tab1", @class: "default-tab")["授权"]]
                            ],
                            div.attr(@class: "clear")
                      ],
                     div.attr(@class: "content-box-content")
                      [
                          div.attr(@class: "tab-content default-tab", id: "tab1")
                          [
                                div.attr(id: "content_box_messages"),
                                form.attr(id: "DroitConfig_Form")
                                [
                                    table.attr(id: "DroitConfig_Table")
                                    [
                                        thead.attr(id: "DroitConfig_Table_thead")
                                        [
                                              tr[th["角色名称"], th["角色描述"], th.attr(colspan: 1, @class: "authtd")["授权"]]
                                        ],
                                        tbody.attr(id: "DroitConfig_Table_tbody")
                                        [
                                               ForEach(Model.RList, (item, c) => c[
                                                    tr[
                                                         th[item.Name],
                                                         th[item.RoleDescription],
                                                            td[
                                                          ForEach(Model.RS, (m, b) => b[

                                                                          label.attr(@class: "label_check", id: "CK_" + item.ID + "_" + m.Key, @for: "RT_" + item.ID + "_" + m.Key)
                                                                          [
                                                                             input.attr(type: InputType.checkbox, @class: "checked", id: "RT_" + item.ID + "_" + m.Key, name: "RT_" + item.ID + "_" + m.Key, value: m.Key.ToString(), @checked: IsChecked(item, m.Key) ? (Checked?)Checked.@checked : null),
                                                                             m.Value
                                                                          ]

                                                              ])
                                                               ]
                                                    ]
                                               ])
                                        ],
                                        tfoot.attr(id: "DroitConfig_Table_tfoot")
                                        [
                                              tr[
                                                 th.attr(scope: Scope.row),
                                                 td,
                                                 td.attr(colspan: Model.RS.Count - 2),
                                                 td.attr(colspan: 2, id: "DroitConfig_Table_Tfoot_Save")
                                                   [
                                                   input.attr(type: InputType.button, id: "DroitConfig_Save", @class: "button", value: "保存授权", onclick: "ConfigDroit();")
                                                   ]
                                              ]
                                        ]
                                    ]
                                ]
                          ]
                      ]
                ]
            ];
        }

        public override ContainerTag CreateTagTree(ContainerTag container)
        {
            return ReusableCreateTagTree(TitleContent, MainContent, Model.MasterViewModel);
        }
    }
}
